package com.megamusic.findshow.controller;

import com.alibaba.fastjson.JSON;
import com.megamusic.findshow.common.constant.SystemConstantsEnum;
import com.megamusic.findshow.common.utils.ResponseUtils;
import com.megamusic.findshow.dao.ContractRepository;
import com.megamusic.findshow.domain.Response;
import com.megamusic.findshow.domain.dto.AreaApplyDto;
import com.megamusic.findshow.domain.dto.ContractDto;
import com.megamusic.findshow.domain.entity.AreaApply;
import com.megamusic.findshow.domain.entity.Contract;
import com.megamusic.findshow.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 找演出商演平台入驻协议
 * --by luxinyu
 */
@Controller
@RequestMapping("/contract")
@Slf4j
public class ContractController {

    @Autowired
    private ContractRepository contractRepository;

    @Autowired
    private SmsService smsService;

    @RequestMapping(value = "show/list",method = {RequestMethod.GET})
    @ResponseBody
    public Response showList(){
        List<Contract> result = contractRepository.findAll();
        return ResponseUtils.getSuccessResponse(result);
    }

    @RequestMapping(value = "addDo", method = {RequestMethod.POST})
    @ResponseBody
    public Response addDo(@RequestBody ContractDto contractDto) {

        //验证验证码
        if (StringUtils.isEmpty(contractDto.getVerifyCode()))
            return ResponseUtils.getErrorResponse(SystemConstantsEnum.VERIFY_CODE_NUll);
        if (StringUtils.isEmpty(contractDto.getPhone()))
            return ResponseUtils.getErrorResponse(SystemConstantsEnum.PHONE_NUll);
        boolean checkResult = smsService.checkVerifyCode(contractDto.getPhone().trim(), contractDto.getVerifyCode().trim());
        if (!checkResult)
            return ResponseUtils.getErrorResponse(SystemConstantsEnum.VERIFY_CODE_WRONG);

        Contract contract = new Contract();
        ;
        contract.setAddress(contractDto.getAddress());
        contract.setContractImg(JSON.toJSON(contractDto.getContractImg()).toString());
        contract.setGender(contractDto.getGender());
        contract.setJob(contractDto.getJob());
        contract.setLeader(contractDto.getLeader());
        contract.setRelationName(contractDto.getRelationName());
        contract.setShopName(contractDto.getShopName());
        contract.setCreateTime(new Date());
        contract.setPhone(contractDto.getPhone());
        log.info("[合同添加]--- param:{}", contractDto.toString());

        contractRepository.save(contract);
        return ResponseUtils.getSuccessResponse("success");
    }


}
